package org.example.utils;

import lombok.extern.slf4j.Slf4j;
import org.example.base.RocketMessage;
import org.example.constants.Constants;
import org.example.factory.messagequeue.CombatFactory;
import org.example.factory.messagequeue.RoomFactory;
import org.example.factory.messagequeue.UserFactory;
import org.example.model.messagequeue.Combat;
import org.example.model.messagequeue.Room;
import org.example.model.messagequeue.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author huangy
 * @description rocketmq测试工具类
 * @since 2022/11/10 15:02
 **/
@Component
@Slf4j
public class RocketMqTestUtils {

    @Resource
    private RocketMqUtils rocketMqUtils;

    @Value("${message.queue.topic}")
    private String topic;

    /*
     * @description 随机消息
     * @author huangy
     * @since 2022/11/10 16:09
     **/
    public void sendRandomMessage() {
        int messageNum = 10;
        //循环发送信息
        for (int i = 0; i < messageNum; i++) {
            //随机tag
            String tag = RandomUtils.randomTag();
            //用户消息
            if (Constants.RocketMqConstants.TAG_TYPE_USER.equals(tag)) {
                RocketMessage<User> rocketMessage = RocketMessage.valueOf(topic, tag);
                rocketMessage.setBody(UserFactory.createUser());
                rocketMqUtils.syncSendMessage(rocketMessage);
            }
            //房间信息
            if (Constants.RocketMqConstants.TAG_TYPE_ROOM.equals(tag)) {
                RocketMessage<Room> rocketMessage = RocketMessage.valueOf(topic, tag);
                rocketMessage.setBody(RoomFactory.createRoom());
                rocketMqUtils.syncSendMessage(rocketMessage);
            }
            //战斗信息
            if (Constants.RocketMqConstants.TAG_TYPE_COMBAT.equals(tag)) {
                RocketMessage<Combat> rocketMessage = RocketMessage.valueOf(topic, tag);
                rocketMessage.setBody(CombatFactory.createCombat());
                rocketMqUtils.syncSendMessage(rocketMessage);
            }
        }
    }

}
